package com.ckd.flyingtrip.shared.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ZoomControls;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.activity.BaseActivity;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.shared.bean.SharedCarOrderDetailBean;
import com.ckd.flyingtrip.utils.Tools;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SharedCarOrderDetailActivity extends BaseActivity {

    @BindView(R.id.toolber_color_title)
    TextView title;
    @BindView(R.id.activity_sharedcar_order_detail_textView1)
    TextView textView1;
    @BindView(R.id.activity_sharedcar_order_detail_textView2)
    TextView textView2;
    @BindView(R.id.activity_sharedcar_order_detail_textView3)
    TextView textView3;
    @BindView(R.id.activity_sharedcar_order_detail_textView4)
    TextView textView4;
    @BindView(R.id.activity_sharedcar_order_detail_textView5)
    TextView textView5;
    @BindView(R.id.activity_sharedcar_order_detail_textView6)
    TextView textView6;
    @BindView(R.id.activity_sharedcar_order_detail_textView7)
    TextView textView7;
    @BindView(R.id.activity_sharedcar_order_detail_textView8)
    TextView textView8;
    @BindView(R.id.activity_sharedcar_order_detail_textView9)
    TextView textView9;
    @BindView(R.id.activity_sharedcar_order_detail_textView10)
    TextView textView10;
    @BindView(R.id.activity_sharedcar_order_detail_textView11)
    TextView textView11;
    @BindView(R.id.activity_sharedcar_order_detail_textView12)
    TextView textView12;
    @BindView(R.id.activity_sharedcar_order_detail_textView13)
    TextView textView13;
    @BindView(R.id.activity_sharedcar_order_detail_textView14)
    TextView textView14;
    @BindView(R.id.activity_sharedcar_order_detail_textView15)
    TextView textView15;
    @BindView(R.id.activity_sharedcar_order_detail_mapView)
    MapView mapView;
    @BindView(R.id.activity_sharedcar_order_detail_ll7)
    LinearLayout ll7;
    @BindView(R.id.activity_sharedcar_order_detail_ll8)
    LinearLayout ll8;
    @BindView(R.id.activity_sharedcar_order_detail_ll9)
    LinearLayout ll9;
    @BindView(R.id.activity_sharedcar_order_detail_ll14)
    LinearLayout ll14;

    private BaiduMap baiduMap;
    private UiSettings mUiSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sharedcar_order_detail);
        ButterKnife.bind(this);

        initView();
        initData();
    }

    private void initView() {
        title.setText("订单详情");

        baiduMap = mapView.getMap();
        mUiSettings = baiduMap.getUiSettings();
        mUiSettings.setAllGesturesEnabled(false);
        mapView.showZoomControls(false);
        mapView.showScaleControl(false);
        View child = mapView.getChildAt(1);
        if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
            child.setVisibility(View.INVISIBLE);
        }
    }

    private void initData() {
        StringRequest request = new StringRequest(Request.Method.GET, Constants.shared_tripdetail + getIntent().getStringExtra("id"), new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                Log.i("订单详情-含轨迹", s);
                SharedCarOrderDetailBean bean = new Gson().fromJson(s, SharedCarOrderDetailBean.class);
                if (bean.getCode().equals("200")) {
                    textView1.setText(bean.getData().getTrip().getId());
                    textView2.setText(bean.getData().getTrip().getBeginTime());
                    textView3.setText(bean.getData().getTrip().getEndTime());
                    textView4.setText(bean.getData().getTrip().getTripTimes() + "s");
                    textView5.setText(bean.getData().getTrip().getTripDistance() + "m");
                    textView6.setText("¥" + bean.getData().getTrip().getStartPrice());
                    if (bean.getData().getTrip().getFencePrice() == null)
                        ll7.setVisibility(View.GONE);
                    else
                        textView7.setText("¥" + bean.getData().getTrip().getFencePrice());
                    if (bean.getData().getTrip().getTimePrice() == null)
                        ll8.setVisibility(View.GONE);
                    else
                        textView8.setText("¥" + bean.getData().getTrip().getTimePrice());
                    if (bean.getData().getTrip().getDistancePrice() == null)
                        ll9.setVisibility(View.GONE);
                    else
                        textView9.setText("¥" + bean.getData().getTrip().getDistancePrice());
                    textView10.setText("¥" + bean.getData().getTrip().getTotalPrice());
                    textView11.setText(bean.getData().getTrip().getPayTranTime());
                    textView12.setText(bean.getData().getTrip().getTripType().equals("0") ? "直接扫码订单" : "预约订单");
                    textView13.setText(bean.getData().getTrip().getStartAddress());
                    if (bean.getData().getTrip().getEndAddress() == null)
                        ll14.setVisibility(View.GONE);
                    else
                        textView14.setText(bean.getData().getTrip().getEndAddress());
                    textView15.setText(bean.getData().getTrip().getPayState().equals("0") ? "未支付" : "已支付");

                    if (bean.getData().getLocus().size() != 0) {
                        List<LatLng> points = new ArrayList<LatLng>();
                        for (int i = 0; i < bean.getData().getLocus().size(); i++) {
                            LatLng latLngA = new LatLng(Double.parseDouble(bean.getData().getLocus().get(i).getLatitude()),
                                    Double.parseDouble(bean.getData().getLocus().get(i).getLongitude()));
                            points.add(latLngA);
                        }
                        OverlayOptions ooPolyline = new PolylineOptions()
                                .width(5)
                                .color(Color.argb(255, 244, 128, 15))
                                .points(points);
                        Polyline mPolyline = (Polyline) baiduMap.addOverlay(ooPolyline);

                        LatLng l = new LatLng(Double.parseDouble(bean.getData().getLocus().get(0).getLatitude()),
                                Double.parseDouble(bean.getData().getLocus().get(0).getLongitude()));
                        MapStatus.Builder builder = new MapStatus.Builder();
                        builder.target(l).zoom(17);
                        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(builder.build());
                        baiduMap.animateMapStatus(mapStatusUpdate);
                    } else {
                        mapView.setVisibility(View.GONE);
                    }
                } else {
                    Tools.toast(bean.getMessage());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismisProgressDialog();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> mHeader = new HashMap<String, String>();
                mHeader.put("TOKEN_XCX_SHARE", SharedCarActivity.token);
                return mHeader;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }

    @OnClick(R.id.toolber_color_back)
    public void onViewClicked() {
        finish();
    }

}
